//
//  DLTFView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/23.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLTFView.h"

@interface DLTFView ()<UITextFieldDelegate>



@end


@implementation DLTFView

- (instancetype) initWithFrame:(CGRect)frame andTitle:(NSString *)title andPlaceHolder:(NSString *)placeHolder {
    if (self = [super initWithFrame:frame]) {
        
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.frame = CGRectMake(20, 30, kScreenWidth - 40, 20);
        titleLabel.text = title;
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = kBoldFont(16);
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UITextField *inputTF = [UITextField new];
        inputTF.frame = CGRectMake(titleLabel.left, titleLabel.bottom, kScreenWidth - 40, 40);
        inputTF.keyboardType = UIKeyboardTypeDefault;
        inputTF.borderStyle = UITextBorderStyleNone;
        inputTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        inputTF.font = kSystemFont(14);
        inputTF.placeholder = placeHolder;
        [inputTF addTarget:self action:@selector(inputTFTextChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:inputTF];
        inputTF.delegate = self;
        self.inputTF = inputTF;
        
        UIView *bottomLine = [UIView new];
        bottomLine.frame = CGRectMake(inputTF.left, inputTF.bottom, titleLabel.width, 0.5);
        bottomLine.backgroundColor = kColorRGB(202, 202, 202);
        [self addSubview:bottomLine];
        
    }
    return self;
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
    
    if ([self.titleLabel.text isEqualToString:@"密码"]) {
        NSLog(@"密码");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TFResignFirstResponder" object:nil];
    }
    if ([self.titleLabel.text isEqualToString:@"新密码"] && [self.inputTF.placeholder isEqualToString:@"请输入6-16位密码"]) {
        NSLog(@"新密码");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TFResignFirstResponder" object:nil];
    }
    if ([self.titleLabel.text isEqualToString:@"确认新密码"]) {
        NSLog(@"确认新密码");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TFResignFirstResponder" object:nil];
    }
}
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
    
    if ([self.titleLabel.text isEqualToString:@"原密码"]) {
        NSLog(@"原密码");
        self.inputTF.keyboardType = UIKeyboardTypeEmailAddress;
        self.inputTF.secureTextEntry = YES;
    } else if ([self.titleLabel.text isEqualToString:@"新密码"]) {
        NSLog(@"新密码");
        self.inputTF.keyboardType = UIKeyboardTypeEmailAddress;
        self.inputTF.secureTextEntry = YES;
        if ([self.inputTF.placeholder isEqualToString:@"请输入6-16位密码"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TFBecomeFirstResponder" object:nil];
        }

    } else if ([self.titleLabel.text isEqualToString:@"确认新密码"]) {
        NSLog(@"确认新密码");
        self.inputTF.keyboardType = UIKeyboardTypeEmailAddress;
        self.inputTF.secureTextEntry = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TFBecomeFirstResponder" object:nil];
    } else if ([self.titleLabel.text isEqualToString:@"手机号"]) {
        NSLog(@"手机号");
        self.inputTF.keyboardType = UIKeyboardTypeNumberPad;
    } else if ([self.titleLabel.text isEqualToString:@"验证码"]) {
        NSLog(@"验证码");
        self.inputTF.keyboardType = UIKeyboardTypeNumberPad;
    } else if ([self.titleLabel.text isEqualToString:@"密码"]) {
        NSLog(@"密码");
        self.inputTF.keyboardType = UIKeyboardTypeEmailAddress;
        self.inputTF.secureTextEntry = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TFBecomeFirstResponder" object:nil];
    }
    return YES;
}

- (void) inputTFTextChange:(UITextField *)textField {
    
    if ([self.titleLabel.text isEqualToString:@"手机号"]) {
        if (textField.text.length >11) {
            NSString *str = textField.text;
            textField.text = [str substringToIndex:11];
        }
    } else if ([self.titleLabel.text isEqualToString:@"验证码"]) {
        
        if (textField.text.length >6) {
            NSString *str = textField.text;
            textField.text = [str substringToIndex:6];
        }
    } else if ([self.titleLabel.text isEqualToString:@"密码"] || [self.titleLabel.text isEqualToString:@"新密码"] || [self.titleLabel.text isEqualToString:@"确认新密码"] || [self.titleLabel.text isEqualToString:@"原密码"]) {
        if (textField.text.length >16) {
            NSString *str = textField.text;
            textField.text = [str substringToIndex:16];
        }
    }
}

- (void) setIsGetSmsCode:(BOOL)isGetSmsCode {
    _isGetSmsCode = isGetSmsCode;
    
    if (isGetSmsCode) {
        
        [self.inputTF resetWidth:kAutoWidth(240)];
        
        UIButton *getVerifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        getVerifyButton.frame = CGRectMake(self.inputTF.right, self.inputTF.top + 5, 95, 30);
//        getVerifyButton.backgroundColor = kDLBtnNormalBgColor;
        [getVerifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        getVerifyButton.titleLabel.font = kSystemFont(14);
        [getVerifyButton setTitleColor:kDLNormalGrayColor forState:UIControlStateNormal];
        [getVerifyButton setTitleColor:kDLBtnNormalBgColor forState:UIControlStateDisabled];
        [getVerifyButton addTarget:self action:@selector(codeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:getVerifyButton];
        getVerifyButton.layer.cornerRadius = 4;
        getVerifyButton.clipsToBounds = YES;
        self.smsCodeBtn = getVerifyButton;
    }
}

- (void) codeBtnClick {
    
    if (self.getVeriftBlock) {
        self.getVeriftBlock();
    }
}

- (void) setIsSecureTextEntry:(BOOL)isSecureTextEntry {
    _isSecureTextEntry = isSecureTextEntry;
    
//    if (isSecureTextEntry) {
//        self.inputTF.secureTextEntry = YES;
//        self.inputTF.keyboardType = UIKeyboardTypeEmailAddress;
//    } else {
//        self.inputTF.secureTextEntry = NO;
//        self.inputTF.keyboardType = UIKeyboardTypeNumberPad;
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
